'use strict';

angular.module('app')
    .controller('subscribeupdateController', ['$scope', '$rootScope', '$state', '$timeout',
        		function($scope, $rootScope, $state, $timeout) {
    	var title = "";
         getselectChannel();
        if ($state.includes('**.subscribe.update')) {
            title = "修改订阅消息";
            var id = $state.params.id;
            validate();
            detail(id);
        } else if ($state.includes('**.subscribe.create')) {
            title = "添加订阅消息";
            validate();
        }
        
        $scope.title = $rootScope.title = title;
        $scope.loading = true;
                     function getselectChannel(){
                         $scope.loading = true;
                            $.ajax({
                                url : $scope.app.project_name+ '/thirdPlatform/channel/selectChannel'
                            }).then(function(result) {
                                $scope.loading = false;
                                if (result.httpCode == 200) {
                                    $scope.channelList = result.data;
                                } else {
                                    $scope.msg = result.msg;
                                }
                                $scope.$apply();
                            });
                        }
                    function validate() {
                        jQuery("form").validate({
                            rules: {
                                channelNo: {
                                    required: true
                                },
                                messageTitle: {
                                    required: true,
                                    maxlength:15
                                },
                                messageComments: {
                                    required: true,
                                    maxlength:50
                                },
                                messageDetail: {
                                    required: true,
                                    maxlength:600
                                },
                                pageStyle: {
                                    required: true
                                },
                                innerUrl: {
                                    required: true,
                                    url:true
                                }
                            },
                            messages: {
                                channelNo: {
                                    required: '渠道来源不能为空'
                                },
                                messageTitle: {
                                    required: '标题不能为空',
                                    maxlength:'标题长度不可超过15'
                                },
                                messageComments:{
                                    required: '消息内容不能为空',
                                    maxlength: '消息内容长度不可超过50'
                                }, messageDetail: {
                                    required: '消息详情不能为空',
                                    maxlength:'消息详情长度不可超过600'
                                }, pageStyle: {
                                    required: '页面样式不能为空'
                                }, innerUrl: {
                                    required: 'url地址不能为空',
                                    url:"格式不正确，网址必须以http://或https://开头"
                                }
                            },
                            submitHandler: function () {
                                $scope.submit();
                            }
                        });
                    }
                    $scope.submit=function(){
                        var chk_value =[];
                        $('input[name="openPlatform"]:checked').each(function(){
                            chk_value.push($(this).val());
                        });
                        $scope.record.openPlatform=chk_value.join(",");
                        console.log($scope.record);
                         var pageStyle=$scope.record.pageStyle;
                         if(pageStyle!=3) {
                             var detailUrlArr = $scope.detailImgData[0];
                             if (!detailUrlArr) {
                                 $scope.alertModal({'content': '请上传消息图标'});
                                 return;
                             }
                             var iconUrl = $scope.detailImgData[0].src;
                             $scope.record.messageIcon = iconUrl[0];
                         }else{
                             $scope.record.messageIcon='';
                         }
                            if(chk_value.length==0){
                                $scope.alertModal({'content': '请选择分发的平台'});
                                return;
                            }
                        var m = $scope.record;
                        console.log($scope.record);
                        if(m){
                            $scope.isDisabled = true;//提交disabled
                            $.ajax({
                                url : $scope.record.subscribeId? $scope.app.project_name + '/subscribe/update' : $scope.app.project_name + '/subscribe/add',
                                data: $scope.record
                            }).then(callback);
                        }

                        function callback(result){
                            console.log(result);
                            if(result.httpCode ==200){//成功
                                    $timeout(function(){
                                        $state.go('main.hui.subscribe.list');
                                    },2000);
                            }else{
                                alert("请求失败");
                                $scope.isDisabled = true;
                            }
                            $scope.loading = false;
                        }
                    }

        // 初始化页面
        function detail(id) {
	        $scope.loading = true;
        	$.ajax({ 
				url : $scope.app.project_name + '/subscribe/read/detail',
				data: {'id': id}
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
                    $scope.record = result.data;
                    var  openPlatforms =  result.data.openPlatform;

                        $("input[name='openPlatform']").each(function(){
                            if ( openPlatforms.indexOf($(this).val())>-1) {
                                this.checked=true;
                            }
                        });


                    $scope.record.subscribeId=$scope.record.id;
                    $scope.detailImgData = [];
                    var detailArr = [];
                    detailArr.push($scope.record.messageIcon);
                    $scope.detailImgData.push({
                        src:detailArr
                    })
                }else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }
                    $scope.alertmes =  function innerUrlFocus(){
            var innerUrl=$("#innerUrl").val();
            console.log(innerUrl);
            if(innerUrl==''){
                $("#innerUrl").val("http://");
            }
        }

        $scope.selected=function platselect(){
            if($("input[name='selectall']").prop("checked")){
            $("input[name='openPlatform']").prop("checked","true");
            }else {
                $("input[name='openPlatform']").removeAttr("checked");
            }
        }

}]);
